package com.sea.supply.controller;

import com.codecrane.core.util.DateUtils;
import com.codecrane.core.util.ExcelUtil;
import com.codecrane.core.web.Ajax;
import com.codecrane.core.web.AjaxReturn;
import com.codecrane.core.web.AjaxTableReturn;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sea.supply.entity.BillRefused;
import com.sea.supply.entity.Buy;
import com.sea.supply.entity.Produce;
import com.sea.supply.service.BillRefusedService;
import com.sea.supply.service.BillService;
import com.sea.supply.service.BuyService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/sea/supply/billRefused")
public class BillRefusedController {

    @Autowired
    private BuyService buyService;
    @Autowired
    private BillRefusedService billRefusedService;

    /**
     * 管理主页面
     * @param model
     * @return
     */
    @RequestMapping("/main")
    public String main(Model model) {

        return "/sea/supply/billRefused/main";
    }

    @RequestMapping("/record")
    public String record(Model model,@RequestParam("refusedPlanNo")String refusedPlanNo) {

        model.addAttribute("refusedPlanNo",refusedPlanNo);
        return "/sea/supply/billRefused/record";
    }

    @RequestMapping("/remark")
    public String remarkPage(Model model,@RequestParam("id")Integer id) {

        return "/sea/supply/billRefused/remark";
    }

    @ResponseBody
    @RequestMapping("/findByCnd")
    public AjaxTableReturn findByCnd(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                     @RequestParam(value = "offset", defaultValue = "0") int start,
                                     BillRefused billRefused) {
        start = start > 0 ? start / pagesize + 1 : 1;
        PageHelper.startPage(start, pagesize);
        List<BillRefused> list = billRefusedService.findByCnd(billRefused);
        PageInfo<BillRefused> page = new PageInfo<>(list);
        return Ajax.tableDataOk(page.getTotal(), page.getList());
    }

    @ResponseBody
    @RequestMapping("/findByCreatePlanNo")
    public AjaxTableReturn findByCreatePlanNo(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                     @RequestParam(value = "offset", defaultValue = "0") int start,
                                              String refusedPlanNo) {
        start = start > 0 ? start / pagesize + 1 : 1;
        PageHelper.startPage(start, pagesize);
        List<BillRefused> list = billRefusedService.findByCreatePlanNo(refusedPlanNo);
        PageInfo<BillRefused> page = new PageInfo<>(list);
        return Ajax.tableDataOk(page.getTotal(), page.getList());
    }

    @ResponseBody
    @RequestMapping(value = "/usave", method = RequestMethod.POST)
    public AjaxReturn usave(BillRefused bill) {
        AjaxReturn result = Ajax.fail().setMsg("修改失败！");
        try{
            if (null != bill && billRefusedService.usave(bill) > 0) {
                result.setOk(true).setMsg("修改成功！");
            }
        }catch (RuntimeException e){
            result.setMsg(e.getMessage());
            return result;
        } catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }


    /**
     * 导出待收货数据
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/export")
    @ResponseBody
    public void exportShipped(HttpServletRequest request, HttpServletResponse response, BillRefused billRefused) {
        List<BillRefused> list = billRefusedService.findByCnd(billRefused);
        String[] title = {"海关单编号","柜号","渠道","收货金额","发票金额","财务审批金额","备注","索赔发票附件","POD附件","关联PO号"};
        String fileName = "发票管理表" + DateUtils.dateToString(new Date(),"yyyyMMdd") + ".xls";
        String sheetName = "发票管理表";
        String[][] content = new String[list.size()][10];
        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[title.length];
            BillRefused obj = list.get(i);
            content[i][0] = obj.getRefusedNo();
            content[i][1] = obj.getRefusedPlanNo();
            content[i][2] = obj.getRefusedChannel();
            content[i][3] = obj.getRefusedCreateMoney()+"";
            content[i][4] = obj.getRefusedBillMoney()+"";
            content[i][5] = obj.getRefusedFinanceMoney()+"";
            content[i][6] = obj.getRefusedRemark();
            content[i][7] = obj.getBillClaimFile();
            content[i][8] = obj.getBillPodFile();
            content[i][9] = obj.getProduceNo();

        }
        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    //发送响应流方法
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


}
